<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="./../template/WorkspaceTemplate.xhtml">

    <ui:define name="top">

    </ui:define>

    <ui:define name="left">

    </ui:define>

    <ui:define name="right">

    </ui:define>

    <ui:define name="content">

        <style>
            .rowHighlight{
                color: red;
                font-weight: bold;
            }
        </style>

        <h:form id="ProductInventoryRecordForm" style="font-size: 85%">
            <h2>View Product Inventory Record</h2>
            <br/>
            <p:panel>
                <p:messages  />
                <br/>
                <h:outputText value="List of Product Inventory Records" style="font-weight: bold; font-size: 110%"/>
                <p:separator/>
                <br/>
                <p:dataTable var="rec" value="#{piManagerBean.allProductInventoryRecord}" emptyMessage="No product inventory records found." rowStyleClass="#{rec.balance le rec.optimalStock?'rowHighlight':null}">  

                    <f:facet name="header">
                        <h:outputText value="Product Inventory Records" />
                    </f:facet>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Product" />  
                        </f:facet>  
                        <h:outputText value="#{rec.product.productType}" />  
                    </p:column>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Optimal Stock Level" />  
                        </f:facet>  
                        <h:outputText value="#{rec.optimalStock}" />  
                    </p:column>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Total" />  
                        </f:facet>  
                        <h:outputText value="#{rec.balance}" />
                    </p:column>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Reserved" />  
                        </f:facet>  
                        <h:outputText value="#{rec.reservedQty}" />
                    </p:column>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Available" />  
                        </f:facet>  
                        <h:outputText value="#{rec.balance - rec.reservedQty}" />
                    </p:column>

                    <p:column>  
                        <f:facet name="header">  
                            <h:outputText value="Options" />  
                        </f:facet>
                        <p:commandButton image="ui-icon ui-icon-pencil" title="Update Optimal Level" actionListener="#{piManagerBean.editOptimalLevel}" update="pirUpdateForm" oncomplete="pirDialog.show()">
                            <f:attribute name="selected" value="#{rec}"/>
                        </p:commandButton>
                        <p:commandButton image="ui-icon ui-icon-search" title="Select Product" actionListener="#{piManagerBean.viewProductInventoryRecord}" update="ProductInventoryRecordForm">
                            <f:attribute name="selected" value="#{rec}"/>
                        </p:commandButton>
                    </p:column>

                </p:dataTable>
                <br/>
                <br/>
                <br/>
                <h:panelGroup id="productBatches" rendered="#{piManagerBean.pir.product != null}">

                    <h:outputText value="#{piManagerBean.pir.product.productType} Inventory" style="font-weight: bold; font-size: 110%"/>
                    <p:separator/>
                    <br/>


                    <p:dataTable var="rec" value="#{piManagerBean.pir.batches}" paginator="true" rows="10">
                        <f:facet name="header">
                            <h:outputText value="#{piManagerBean.pir.product.productType} Batch Information" />
                        </f:facet>

                        <p:column headerText="Batch No.">
                            <h:outputText value="#{rec.batchNo}"/>
                        </p:column>

                        <p:column headerText="Manufacture Date">
                            <h:outputText value="#{rec.dateManufactured}">
                                <f:convertDateTime pattern="MMM dd, yyyy"/>
                            </h:outputText>
                        </p:column>

                        <p:column headerText="Shelf Date">
                            <h:outputText value="#{rec.dateShelf}">
                                <f:convertDateTime pattern="MMM dd, yyyy"/>
                            </h:outputText>
                        </p:column>

                        <p:column headerText="Printed Expiry Date">
                            <h:outputText value="#{rec.datePrinted}">
                                <f:convertDateTime pattern="MMM dd, yyyy"/>
                            </h:outputText>
                        </p:column>

                        <p:column headerText="Expiry Date">
                            <h:outputText value="#{rec.dateExpired}">
                                <f:convertDateTime pattern="MMM dd, yyyy"/>
                            </h:outputText>
                        </p:column>

                        <p:column headerText="Total Quantity">
                            <h:outputText value="#{rec.quantity}"/>
                        </p:column>
                        
                        <p:column headerText="Bin Location">
                            <p:commandButton image="ui-icon ui-icon-home" title="Bin Location" actionListener="#{piManagerBean.viewBatchLocation}" update="ProductInventoryRecordForm">
                                <f:attribute name="selected" value="#{rec}"/>
                            </p:commandButton>
                        </p:column>

                    </p:dataTable>
                </h:panelGroup>
                <br/>
                <br/>
                <br/>
                
                <h:panelGroup id="batchLocation" rendered="#{piManagerBean.fg != null}">

                    <h:outputText value="#{piManagerBean.pir.product.productType} Batch #{piManagerBean.fg.batchNo} Location" style="font-weight: bold; font-size: 110%"/>
                    <p:separator/>
                    <br/>

                    <p:dataTable var="rec" value="#{piManagerBean.fg.locations}">
                        <f:facet name="header">
                            <h:outputText value="Warehouse Locations" />
                        </f:facet>

                        <p:column headerText="Location">
                            <h:outputText value="#{rec.binLocation}"/>
                        </p:column>

                        <p:column headerText="Bin Capacity">
                            <h:outputText value="#{rec.binCapacity}"/>
                        </p:column>
                        
                        <p:column headerText="Stored Quantity">
                            <h:outputText value="#{rec.storedQty}"/>
                        </p:column>
                    </p:dataTable>
                </h:panelGroup>
                <br/>
                <br/>
                <br/>
            </p:panel>
            <br/>
            <br/>
            <br/>
        </h:form>

        <script type="text/javascript">  
            function handleUpdatePIR(xhr, status, args) {  
                if(!args.valid) {  
                    jQuery('#pirDialog').parent().effect("shake", { times:3 }, 100);
                } else {  
                    pirDialog.hide();
                }  
            }  
        </script>

        <p:dialog header="Update Optimal Stock Level" width="600" id="pirDialog" widgetVar="pirDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false" closable="false">
            <h:form id="pirUpdateForm" style="font-size: 85%">
                <br/>
                <p:messages  />
                <h:panelGrid columns="2" cellpadding="10">
                    <h:outputLabel value="Product Inventory: " style="font-weight: bold"/>
                    <h:outputText value="#{piManagerBean.pir.product.productType}"/>
                    <h:outputLabel value="Optimal Stock Level: " style="font-weight: bold"/>
                    <h:inputText value="#{piManagerBean.pir.optimalStock}" required="true" requiredMessage="Optimal cannot be empty"/>
                </h:panelGrid>
                <br/>
                <br/>
                <div align="right">
                    <p:commandButton value="Cancel" image="ui-icon ui-icon-close" oncomplete="pirDialog.hide()"/>
                    <p:commandButton value="Update" image="ui-icon ui-icon-disk" oncomplete="handleUpdatePIR(xhr, status, args)" action="#{piManagerBean.saveProductInventoryGroup()}" update="pirUpdateForm ProductInventoryRecordForm"/>
                </div>
            </h:form>
        </p:dialog>

    </ui:define>

    <ui:define name="bottom">

    </ui:define>

</ui:composition>
